﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using EPiServer.Core;

namespace EPiServer.Municipality.Templates.Pages {
    public partial class Index : TemplatePage {
        /* *******************************************************************
         * Properties
         * *******************************************************************/
        #region protected PageReference StartingPoint
        /// <summary>
        /// Gets the StartingPoint of the Index
        /// </summary>
        /// <value></value>
        protected PageReference StartingPoint {
            get {
                return (CurrentPage["IndexContainer"] as PageReference) ?? PageReference.StartPage;
            }
        }
        #endregion
        /* *******************************************************************
         * Methods
         * *******************************************************************/
        #region override protected void OnInit(EventArgs e)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        override protected void OnInit(EventArgs e) {
            InitializeComponent();
            base.OnInit(e);
        }
        #endregion
        #region private void InitializeComponent()
        /// <summary>
        /// 
        /// </summary>
        private void InitializeComponent() {
            Load += Page_Load;
        }
        #endregion
        #region protected string GetPrefix(string pageName)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageName"></param>
        /// <returns></returns>
        protected static string GetPrefix(string pageName) {
            if (pageName != string.Empty)
                return pageName.Substring(0, 1).ToUpper();
            return String.Empty;
        }
        #endregion
        #region private PageDataCollection GetPages()
        private PageDataCollection GetPages() {
            var pages = new PageDataCollection();
            var propcol = new PropertyCriteriaCollection();
            var criteria = new PropertyCriteria {
                Type = PropertyDataType.Boolean,
                Name = "VisibleInIndex",
                Value = true.ToString(),
                Condition = Filters.CompareCondition.Equal,
                Required = true
            };

            propcol.Add(criteria);

            var tempPages = DataFactory.Instance.FindPagesWithCriteria(StartingPoint, propcol, CurrentPage.LanguageBranch);

            // Check pages are published
            foreach (PageData p in tempPages) {
                if (p.Status == VersionStatus.Published)
                    pages.Add(p);
            }
            tempPages.Clear();
            return pages;
        }
        #endregion
        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
        #region protected void Page_Load(object sender, EventArgs e)
        /// <summary>
        /// This method is called when the Page's Load event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> of the event.</param>
        protected void Page_Load(object sender, EventArgs e) {
            IndexListControl.DataSource = GetPages();
            IndexListControl.DataBind();
        }
        #endregion
       
    }
}